package com.ruoyi.web.controller.commodity;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.commodity.domain.vo.ProductVO;
import com.ruoyi.commodity.service.*;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.commodity.service.AuthBrandService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.commodity.domain.Product;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.ruoyi.common.core.page.TableSupport.PAGE_NUM;
import static com.ruoyi.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 产品管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-09
 */
@RestController
@RequestMapping("/commodity/products")
public class ProductController extends BaseController
{
    @Autowired
    private IProductService productService;

    @Autowired
    private IproductCatesService productCatesService;;

    @Autowired
    private IOemService oemService;

    @Autowired
    private IColorService colorService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private AuthBrandService authBrandService;

    /**
     * 查询产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductVO product) {
        // 调用业务层方法，返回 PageInfo 对象
        PageInfo<ProductVO> pageInfo = productService.selectProductList(product);
        // 将分页数据和总记录数封装返回
        return new TableDataInfo(pageInfo.getList(), (int)pageInfo.getTotal());
    }
    /**
     * 导出产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductVO product)
    {
        List<ProductVO> list = productService.selectProductList(product).getList();
        ExcelUtil<ProductVO> util = new ExcelUtil<ProductVO>(ProductVO.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取产品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }



    /**
     * 新增产品管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductVO product)
    {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改产品管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductVO product)
    {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 停用产品管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:disable')")
	@PutMapping("/disable")
    public AjaxResult disable(@RequestBody List<Long> ids)
    {
        return toAjax(productService.disable(ids));
    }

    /**
     * 启用产品管理
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:enable')")
	@PutMapping("/enable")
    public AjaxResult enable(@RequestBody List<Long> ids)
    {
        return toAjax(productService.enable(ids));
    }

    /**
     * 获取产品树列表
     * @param productCates
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:list')")
    @GetMapping("/productTree")
    public AjaxResult productTree(ProductCates productCates){
        return success(productCatesService.selectProductTreeList(productCates));
    }

    /**
     * 获取oem树列表
     * @param oem
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:list')")
    @GetMapping("/oemTree")
    public AjaxResult ormTree(Oem oem){
        return success(oemService.selectOemTree(oem));
    }

    /**
     * 生产sku
     * @param oem
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:query')")
    @GetMapping("/skuName/{oem}")
    public AjaxResult skuName(@PathVariable Long oem){
        return success(productService.skuName(oem));
    }

    /**
     * 获取所以color
     * @param color
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:list')")
    @GetMapping("/selectColor")
    public AjaxResult selectColor(Color color){
        return success(colorService.selectColorTree(color));
    }

    @GetMapping("/selectColorById/{id}")
    public AjaxResult selectColorById(@PathVariable Long id){
        return success(colorService.selectColorById(id));
    }

    /**
     * 获得所有品牌
     * @param brand
     * @return
     */
    @GetMapping("/selectBrand")
    public AjaxResult selectBrand(AuthBrand brand){
        return success(authBrandService.selectBrandTreeList(brand));
    }

    /**
     * 查询商品要关联的商品
     * @param product
     * @return
     */
    @GetMapping("/selectProductBrand")
    public TableDataInfo selectProductBrand(Product product){
        PageInfo<Product> pageInfo = productService.selectProductBrand(product);
        return new TableDataInfo(pageInfo.getList(),(int)pageInfo.getTotal());
    }

    /**
     * 查询销售订单下的商品
     * @param product
     * @return
     */
    @GetMapping("/selectByPlanId")
    public TableDataInfo selectByPlanId(@ModelAttribute Product product, @RequestParam(required = false) Boolean isEdit){
        startPage();
        return getDataTable(productService.selectByPlanId(product, isEdit));
    }

    @GetMapping("/selectById/{id}")
    public AjaxResult selectById(@PathVariable Long id){
        return success(brandService.selectById(id));
    }

    /**
     * 导入产品表格数据
     * @param file
     * @return
     */
    @PreAuthorize("@ss.hasPermi('commodity:products:importProduct')")
    @PostMapping("/importProductExcel")
    public String importProductExcel(@RequestParam("file") MultipartFile file) {
        int row = productService.importVendorExcel(file);
        return "Excel 导入成功，新增产品：" +row + " 条";
    }

}
